<?php
namespace User\Controller;
use Common\Controller\AdminbaseController;


class UsergroupController extends AdminbaseController {
	protected $user_group_model,$region_model;

	function _initialize() {
		parent::_initialize();
		$this->user_group_model = D("UserGroup");
		$this->region_model = D("Region");

	}
	function index(){
		$this->_lists();
		$this->display();
	}
	function _lists(){
		if(!get_current_adminug_id() && get_current_adminrole_id() > 9){
            $search[] = array('id'=>0);
            $_GET['id'] = 0;
		}elseif(get_current_adminug_id() && get_current_adminrole_id() > 9){
            $search[] = array(array('id'=>get_current_adminug_id()),
				'_logic'=>"OR",
				array('parentid'=>get_current_adminug_id())
			);
            $_GET['id'] = get_current_adminug_id();
            $_GET['parentid'] = get_current_adminug_id();
		}
		$count = $this->user_group_model->where($search)->count();
		$page = $this->page($count, 50);
		$ugroups = $this->user_group_model->where($search)->order("id DESC")->select();
		$this->assign("ugroups", $ugroups);
		$this->assign("page", $page->show('Admin'));
	}

	function add(){
		$province = $this->region_model->where(array('parent_id'=>100000))->select();
		$this->assign("province", $province);
		$this->display();
	}
	function add_post(){
		if(get_current_adminug_id() && get_current_adminrole_id() == 10){
			$this->error("您已经在小组中，无需再次创建");
		}
		if(get_current_adminug_id() && get_current_adminrole_id() == 20){
			$this->error("您已经在分组中，无需再次创建");
		}
		if(IS_POST){
			if($this->user_group_model->where(array('name'=>I("post.name")))->find()){
				$this->error("小组名称已存在，请换个名字试试");
			}
			if($_FILES){
				$Aliyun = new \Aliyun\Controller\AliyunController();
				if($_FILES["logo"]){
					$logo = $Aliyun->save($_FILES["logo"],'img/'.CONTROLLER_NAME,0,'128');
					if(!$logo['error']){
						$_POST['logo'] =$logo['path'].$logo['name'];
					}else{
						$this->error('图像上传失败');
					}
				}
			}
			$_POST['status']=0;
			$_POST['create']=date("Y-m-d H:i:s");
			$_POST['modify']=date("Y-m-d H:i:s");
			$_POST['userid']=get_current_admin_id();
			$ugroup = $this->user_group_model->add($_POST);
			if($ugroup){
				$_SESSION['UG_ID'] = $ugroup;
				$user['province']=I("post.province");
				$user['ugid']=$ugroup;
				D("Common/Users")->where(array('id'=>get_current_admin_id()))->save($user);
				$this->success("小组创建成功！",U('Admin/Main/index'));
			}else{
				$this->error("小组创建失败");
			}
		}
	}

	function edit(){
		$id = intval(I("get.id"));
		$ugroup = $this->user_group_model->where(array("id"=>$id))->find();
		$province = $this->region_model->where(array('code'=>$ugroup['province']))->find();
		$this->assign("province", $province);
		$this->assign("ugroup", $ugroup);
		$this->assign("urlimg", C('IMG_PATH'));
		$this->display();
	}
	function edit_post(){
		if(IS_POST){
			if($_FILES){
				$Aliyun = new \Aliyun\Controller\AliyunController();
                if (!empty($_FILES["newlogo"]) && !empty($_POST["logo"])) {
					$Aliyun->delete($_POST['logo']);
				}
				if($_FILES["newlogo"]){
					$logo = $Aliyun->save($_FILES["newlogo"],'img/'.CONTROLLER_NAME,0,'128');
					if(!$logo['error']){
						$_POST['logo'] =$logo['path'].$logo['name'];
					}else{
						$this->error('图像上传失败');
					}
				}
			}
			$_POST['modify']=date("Y-m-d H:i:s");
			$_POST['userid']=get_current_admin_id();
			if($this->user_group_model->save($_POST)){
				$this->success("小组修改成功！",U('Admin/Main/index'));
			}else{
				$this->error("小组修改失败");
			}
		}
	}

    public function del_img(){
    	if (I("get.id")) {
    		$user_group = $this->user_group_model->where(array('id'=>I("get.id")))->find();
			if($user_group['logo']){
				$Aliyun = new \Aliyun\Controller\AliyunController();
				$Aliyun->delete($user_group['logo']);
			}
    		if ($this->user_group_model->where(array('id'=>I("get.id")))->setField('logo',NULL)) {
    			$this->success("图片删除成功！");
    		} else {
    			$this->error('图片删除失败！');
    		}
    	} else {
			$this->error($this->user_group_model->getError());
    	}
    }
	public function up(){
    	if (I("get.id")) {
    		if ($this->user_group_model->where(array('id'=>I("get.id")))->setField('status','1')) {
    			$this->success("小组启用成功！");
    		} else {
    			$this->error('小组启用失败！');
    		}
    	} else {
			$this->error($this->user_group_model->getError());
    	}
    }
    public function down(){
    	if (I("get.id")) {
    		if ($this->user_group_model->where(array('id'=>I("get.id")))->setField('status','2')) {
    			$this->success("小组停用成功！");
    		} else {
    			$this->error('小组停用失败！');
    		}
    	} else {
			$this->error($this->user_group_model->getError());
    	}
    }
}